package com.exp.sys.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import tk.mybatis.mapper.entity.Example;

import com.alibaba.druid.support.json.JSONUtils;
import com.exp.base.action.DataTablesPage;
import com.exp.base.action.DataTablesPageInfo;
import com.exp.base.action.EasyuiPage;
import com.exp.base.action.EasyuiPageInfo;
import com.exp.base.action.Message;
import com.exp.sys.entity.Admin;
import com.exp.sys.entity.Role;
import com.exp.sys.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/sys/role")
public class RoleAction extends BaseAction {
	@Resource
	RoleService roleDomain;
	
	@RequestMapping("add")
	public String add(Model model){
		Role role = new Role();
		model.addAttribute("role", role);
		return "sys/role/role_input";
	}
	
	@RequestMapping("edit")
	public String edit(Model model,@ModelAttribute("id") String id){
		Role role = roleDomain.get(id);
		model.addAttribute("role", role);
		return "sys/role/role_input";
	}
	
	@RequestMapping("list")
	public String list(){
		return "sys/role/role_list";
	}
	
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(HttpServletRequest request,Role role){
		List<String> errors = new ArrayList<String>();
		if(isValid(role, errors)){
		}
		if(errors.isEmpty()){
			Example example = new Example(Admin.class);
			example.createCriteria().andEqualTo("name", role.getName());
			if(roleDomain.exist(example)){
				errors.add(StringUtils.join(role.getName(),"已存在",(role.isDeleted()?"（回收站）":"")));
			};
		}
		if(errors.isEmpty()){
			roleDomain.save(role);
			return new Message(true);
		}
		return new Message(false,StringUtils.join(errors,","));
	}
	
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(HttpServletRequest request,Role role){
		List<String> errors = new ArrayList<String>();
		if(isValid(role, errors)){
			Role roleEntity = roleDomain.get(role.getId());
			filterParameterMapIgnore(request, role, roleEntity);
			roleDomain.update(roleEntity);
			return new Message(true);
		}else{
			return new Message(false,StringUtils.join(errors,","));
		}
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(HttpServletRequest request, String[] ids){
		roleDomain.deleteWithRelate(ids);
		return new Message(true);
	}
	
	@ResponseBody
	@RequestMapping(value="softDelete")
	public Message softDelete(HttpServletRequest request, String[] ids){
		roleDomain.softDelete(ids);
		return new Message(true);
	}
	
	@ResponseBody
	@RequestMapping("query")
	public EasyuiPageInfo query(Model model,EasyuiPage easyuiPage){
		Page<Role> page = new Page<Role>(easyuiPage.getPage(), easyuiPage.getRows());
		Example example = new Example(Role.class);
		example.createCriteria().andEqualTo("deleted", false);
		example.setOrderByClause("create_date desc");
		PageInfo<Role>  pageInfo = roleDomain.query(example, page);
		EasyuiPageInfo easyuiPageInfo = new EasyuiPageInfo(pageInfo);
		return easyuiPageInfo;
	}
	
	@ResponseBody
	@RequestMapping("queryRecycle")
	public EasyuiPageInfo queryRecycle(Model model,EasyuiPage easyuiPage){
		Page<Role> page = new Page<Role>(easyuiPage.getPage(), easyuiPage.getRows());
		Example example = new Example(Role.class);
		example.createCriteria().andEqualTo("deleted", true);
		example.setOrderByClause("create_date desc");
		PageInfo<Role> pageInfo = roleDomain.query(example, page);
		EasyuiPageInfo easyuiPageInfo = new EasyuiPageInfo(pageInfo);
		return easyuiPageInfo;
	}
	
	
	
	
}
